<?php namespace App\Http\Controllers;

use App\Article;
use App\Department;
use App\DocHug;
use App\Doctors;
use App\Exceptions\NotFoundException;
use App\Goods;
use App\GoodsImg;
use App\Hospital;
use App\OrderItem;
use App\Orders;
use App\Packages;
use App\phoneMsg;
use App\Referral;
use App\Services\UserManager;
use App\User;
use App\UserAddress;
use App\UserInfo;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use Cookie;

class AdminController extends Controller {

    public function login(){
        return view('admin.login');
    }

    public function index(Guard $guard,Request $request){
        $user = UserManager::isLogin($request);
        if(!$user)
            return redirect(route('Admin.Index.login'));

        return view('index');
    }

    public function userList(){
        $allUsers = UserInfo::orderBy('created_at','desc')->paginate(50);

        foreach($allUsers as $userinfo){
            $userinfo->uid = $userinfo->user->uid;
        }
        return view('admin.user-list',['userinfos'=>$allUsers]);
    }

    public function packagesList(){
        $packages = Packages::skip(0)->take(50)->orderBy('created_at','desc')->paginate(50);
        return view('admin.packages-list',['packages'=>$packages]);
    }

    public function packagesAdd(){
        $id = Input::get('id');
        if ($id)
            $packages = Packages::find($id);
        else
            $packages = '';


        return view('admin.packages-add',['package'=>$packages]);
    }
    
    public function doctorList(){
        $doctors = Doctors::orderBy('created_at','desc')->paginate(50);

        if(count($doctors) > 0){
            foreach ($doctors as $doctor){
                /** @var Doctors $doctor */
                $thatHospital = Hospital::whereId($doctor->hospital_id)->first();
                if($thatHospital){
                    $doctor->hospital_name = $thatHospital->name;
                }else{
                    $doctor->hospital_name = "";
                }

                $thatDep = Department::whereId($doctor->dep_id)->first();
                if($thatDep){
                    $doctor->dep_name = $thatDep->dep_name;
                }else{
                    $doctor->dep_name = "";
                }

            }
        }

        return view('admin.doctor-list',['doctors'=>$doctors]);
    }

    public function doctorAdd(){
        $id = Input::get('id');
        $thatDoctor = Doctors::find($id);

        $allHostpital = Hospital::all();

        if($thatDoctor){
            $allDep = Department::whereHospitalId($thatDoctor->hospital_id)->get();
        }else{
            $allDep = Department::whereHospitalId($allHostpital[0]['id'])->get();
        }


        return view('admin.doctor-add',['doctor'=>$thatDoctor,"hospitals"=>$allHostpital,"deps"=>$allDep]);
    }

    public function doctorHug(){
        $defaultTime = date('Y-m-d',time()+86400);
        $date = !is_null(Input::get('date')) ? Input::get('date') : $defaultTime;
        $id   = Input::get('id');

        $thatDoctor = Doctors::find($id);
        $doctorHugs = DocHug::whereHughDate($date)->whereDocId($id)->get();

//        dd($doctorHugs);die;
        $a = array();
        for($i = 0;$i<6;$i++){
            $flag = 0;
            foreach($doctorHugs as $k => $v){
                if($i+1 == (int)$v['hugh_slot']){
                    $flag = 1;
                }
            }
            array_push($a,$flag);
        }

        $times = array('9:00-10:00','10:00-11:00 ','11:00-12:00','14:30-15:30','15:30-16:30','16:30-17:30');

        $week = mb_substr( "日一二三四五六",date("w",strtotime($date)),1,"utf-8" );
        return view('admin.doctor-hug',['date'=>$date,'week'=>$week,'id'=>$id,'hugs'=>$a,'times'=>$times]);
    }

    public function createContent(){
        $contentid = Input::get('id');

        $thatContent = Article::find($contentid);

        return view('admin.create-content',['content'=>$thatContent]);
    }

    public function createGood(){
        $id = Input::get('id');

        /** @var Goods $thatGood */
        $thatGood = Goods::find($id);

        $thatImgs = GoodsImg::whereGoodId($id)->get();

        if(count($thatImgs) !== 0){
            $thatGood->img1 = count($thatImgs) >= 1 ? $thatImgs[0]->thumb_id : 0;
            $thatGood->img2 = count($thatImgs) >= 2 ? $thatImgs[1]->thumb_id : 0;
        }

        return view('admin.create-good',['good'=>$thatGood]);
    }

    public function phoneMsg(){
        $allPhoneMsg = phoneMsg::all();

        return view('admin.phone-msg',['phones'=>$allPhoneMsg]);
    }

    public function orderList(){

        $orders = Orders::whereIspay(1)->orderBy('created_at','desc')->paginate(50);;

        foreach($orders as $order){
            /** @var Orders $order */
            if($order->type == 'package'){
                /** @var Packages $thatPacket */
                $thatPacket = Packages::find($order->package_id);
                if($thatPacket){
                    $order->title = $thatPacket->pak_name."【缴费项】";
                }else{
                    $order->title = "该缴费项已经被删除";
                }

            }elseif($order->type == "face"){
                /** @var Doctors $thatDoctor */
                $thatDoctor = Doctors::whereUserId($order->doctor_id)->first();
                if($thatDoctor){
                    $order->title = $thatDoctor->doc_name."医生【专家面诊】";
                }else{
                    $order->title = "该医生资料已被删除";
                }
            }elseif ($order->type == "appoint"){
                /** @var Doctors $thatDoctor */
                $thatDoctor = Doctors::whereUserId($order->doctor_id)->first();
                if($thatDoctor){
                    $order->title = $thatDoctor->doc_name."医生【普通门诊】";
                }else{
                    $order->title = "该医生资料已被删除";
                }
            }

            /** @var UserInfo $thatUserInfo */
            $thatUserInfo = UserInfo::whereUserId($order->user_id)->first();

            $order->nickname = $thatUserInfo ? $thatUserInfo->nick : "";
            $order->sexual   = $thatUserInfo ? $thatUserInfo->sexual : "";
            $order->user_img = $thatUserInfo ? $thatUserInfo->portrait_id : "";
            $order->uid      = $thatUserInfo ? $thatUserInfo->user->uid : "";
        }

        $times = array('9:00-10:00','10:00-11:00 ','11:00-12:00','14:30-15:30','15:30-16:30','16:30-17:30');

        return view('admin.order-list',['orders'=>$orders,'times'=>$times]);
    }

    public function keshiList(){
        $allKeshi = Article::orderBy('created_at','desc')->paginate(50);
        return view('admin.keshi-list',["keshis"=>$allKeshi]);
    }

    public function changePwd(){
        return view('admin.change-psw');
    }

    /** 登出 */
    public function signOut(Guard $guard,Request $request)
    {
        $cookie = Cookie::forget('user_token');

        return redirect(route('Admin.Index.login'))->withCookie($cookie);
    }

    public function doctorDetail(Guard $guard,Request $request){
        $user = UserManager::isLogin($request);
        if(!$user)
            return redirect(route('Admin.Index.login'));


        $id = Input::get('id');

        /** @var Doctors $thatDoctor */
        $thatDoctor = Doctors::whereUserId($id)->first();


        $thatHostipal = Hospital::whereId($thatDoctor->hospital_id)->first();


        if($thatHostipal)
            $thatDoctor->hospital = $thatHostipal->name;
        else
            $thatDoctor->hospital = '';

        return view('admin.doctor-detail',['doctor'=>$thatDoctor]);
    }

    public function hospitalList(Guard $guard,Request $request){
        $user = UserManager::isLogin($request);
        if(!$user)
            return redirect(route('Admin.Index.login'));

        $allHospital = Hospital::orderBy('sort','desc')->paginate(20);
        if(count($allHospital) > 0){
            foreach ($allHospital as $hospital){
                $keshis = Department::whereHospitalId($hospital->id)->get();
                $hospital->keshis = $keshis;
            }
        }

        return view("admin.hospital-list",["hospitals"=>$allHospital]);
    }

    public function hospitalAdd(Guard $guard,Request $request){
        $user = UserManager::isLogin($request);
        if(!$user)
            return redirect(route('Admin.Index.login'));

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;
        $thatHospital = Hospital::whereId($id)->first();

        return view("admin.hospital-add",["hospital"=>$thatHospital]);
    }

    public function referralList(Guard $guard,Request $request){
        $user = UserManager::isLogin($request);
        if(!$user)
            return redirect(route('Admin.Index.login'));

        $allReferral = Referral::orderBy('id',"desc")->paginate(20);

        if(count($allReferral) > 0){
            foreach ($allReferral as $referral){
                /** @var Referral $referral */
                $thatDoctor = Doctors::whereUserId($referral->d_user_id)->first();
                if($thatDoctor){
                    $thatHospital = Hospital::whereId($thatDoctor->hospital_id)->first();
                    if($thatHospital){
                        $thatDoctor->hospital_name = $thatHospital->name;
                    }else{
                        $thatDoctor->hospital_name = "";
                    }
                }

                $referral->doctor_info = $thatDoctor;

                $thatReceiveDoctor = Doctors::whereUserId($referral->dr_user_id)->first();
                if($thatReceiveDoctor){
                    $thatHospital = Hospital::whereId($thatReceiveDoctor->hospital_id)->first();
                    if($thatHospital){
                        $thatReceiveDoctor->hospital_name = $thatHospital->name;
                    }else{
                        $thatReceiveDoctor->hospital_name = "";
                    }
                }

                $referral->redoctor_info = $thatReceiveDoctor;

                $order = Orders::whereOrderNum($referral->order_num)->first();

                $referral->order_info = $order;

                $thatUserInfo = UserInfo::whereUserId($referral->user_id)->first();
                $referral->user_info = $thatUserInfo;
            }
        }

        return view("admin.referral-list",["referrals"=>$allReferral]);
    }
}